let db = require("../db/index");

exports.all = (req, res) => {
  //获取joinclub表全部数据
  var sql = "select * from joinclub_yang";
  db.query(sql, (err, data) => {
    if (err) {
      return res.send("错误：" + err.message);
    }
    res.send(data);
  });
};
exports.get = (req, res) => {
  //通过id查询数据
  var sql = "select * from joinclub_yang where student = ? or studentid = ?"; //？用于占位
  db.query(sql, [req.query.student, req.query.studentid], (err, data) => {
    if (err) {
      return res.send("错误：" + err.message);
    }
    res.send(data);
  });
};

exports.del = (req, res) => {
  //通过id删除数据
  var sql = "delete from joinclub_yang where id = ?";
  db.query(sql, [req.query.id], (err, data) => {
    if (err) {
      return res.send("错误：" + err.message);
    }
    res.send(data);
  });
};

exports.add = (req, res) => {
  //向joinclub表添加数据
  var sql =
    "insert into joinclub_yang (id,student,club,self,why,date,state,studentid,new) values (?,?,?,?,?,?,?,?,?)";
  db.query(
    sql,
    [
      req.query.id,
      req.query.student,
      req.query.club,
      req.query.self,
      req.query.why,
      req.query.date,
      req.query.state,
      req.query.studentid,
      req.query.new,
    ],
    (err, data) => {
      if (err) {
        return res.send("错误：" + err.message);
      }
      res.send(data);
    }
  );
};

exports.update = (req, res) => {
  //通过id更新数据
  var sql =
    "update joinclub_yang set student = ?, club = ?, self = ?, why = ?, date = ?, state = ?, studentid = ?, new = ? where id = ?";
  db.query(
    sql,
    [
      req.query.student,
      req.query.club,
      req.query.self,
      req.query.why,
      req.query.date,
      req.query.state,
      req.query.studentid,
      req.query.new,
      req.query.id,
    ],
    (err, data) => {
      if (err) {
        return res.send("错误：" + err.message);
      }
      res.send(data);
    }
  );
};
